package com.myideas.webapp.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.myideas.model.Knowledge;
import com.myideas.model.LabelValue;
import com.myideas.model.User;
import com.myideas.service.KnowledgeManager;
import com.myideas.util.ConvertUtil;
 
public class KnowledgeForm extends BasePage implements Serializable {
	private KnowledgeManager knowledgeManager;
    private Knowledge knowledge = new Knowledge();
    private String id;
    private List<LabelValue> users = new ArrayList<LabelValue>();
//    private Map<String, String> availableKnowledges;
	private Long user_fk;
    
    public void setKnowledgeManager(KnowledgeManager knowledgeManager) {
        this.knowledgeManager = knowledgeManager;
    }
 
    public Knowledge getKnowledge() {
        return knowledge;
    }

    public void setKnowledge(Knowledge knowledge) {
        this.knowledge = knowledge;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String delete() {
        knowledgeManager.removeKnowledge(knowledge.getId().toString());
        addMessage("knowledge.deleted", knowledge.getId().toString()); 

        return "list";
    }

    public String edit() {
        if (id != null) {
            knowledge = knowledgeManager.getKnowledge(id);
        } else {
            knowledge = new Knowledge();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (knowledge.getId() == null);
        addMessage("knowledge.selectedUserIgnored");

		//Merges instance into the database persistence manager and updates the reference
		//to be used by JSF to generate the screen. Necessary to update any possible 
		//modifications imposed by the hibernate engine or the database over the entity
		//(like increasing its version, for instance)
        knowledge = knowledgeManager.save(knowledge, user_fk); 

        String key = (isNew) ? "knowledge.added" : "knowledge.updated";
        addMessage(key, new Object[] {knowledge.getId(), knowledge.getVersion()});

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
    
    // Form Controls ==========================================================
    @SuppressWarnings("unchecked")
    public Map<String, String> getAvailableUsers() {
//    	Map<String, String> availableUsers;
        List l = knowledgeManager.getAvailableUsers();
        for (Iterator iterator = l.iterator(); iterator.hasNext();) {
			User user = (User) iterator.next();
			users.add(new LabelValue(user.getUsername(), user.getId().toString()));
		}
//        
//            for (User user : l) {
//                // convert the user's roles to LabelValue Objects
//            }
//    	availableUsers = ConvertUtil.convertListToMap(users);

    	return ConvertUtil.convertListToMap(users);
    }

	public Long getUser_fk() {
		return knowledge != null ? knowledge.getUser() != null ? knowledge.getUser().getId() : null : null;
	}

	public void setUser_fk(Long user_fk) {
		this.user_fk = user_fk;
	}


} 